package com.ict.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ict.common.core.domain.entity.SysDept;
import com.ict.common.utils.SecurityUtils;
import com.ict.common.utils.StringUtils;
import com.ict.common.utils.uuid.IdUtils;
import com.ict.system.mapper.SysUserRoleMapper;
import com.ict.system.service.ISysDeptService;
import com.ict.web.constant.Constants;
import com.ict.web.constant.ProjectStatusEnum;
import com.ict.web.controller.file.domain.YzqFile;
import com.ict.web.controller.file.service.IYzqFileService;
import com.ict.web.domain.*;
import com.ict.web.dto.LastCheckAddDto;
import com.ict.web.dto.LastCheckEditDto;
import com.ict.web.mapper.LastCheckMapper;
import com.ict.web.mapper.ProjectManagementMapper;
import com.ict.web.msg.domain.SysMsg;
import com.ict.web.msg.util.CreateMsgUtil;
import com.ict.web.service.IApprovalService;
import com.ict.web.service.ILastCheckService;
import com.ict.web.service.IProjectManagementService;
import com.ict.web.service.IReviewTaskService;
import com.ict.web.vo.LastCheckDetailsVo;
import com.ict.web.vo.LastCheckTimeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * 终验备案管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-14
 */
@Service
public class LastCheckServiceImpl extends ServiceImpl<LastCheckMapper, LastCheck> implements ILastCheckService
{
    @Resource
    private LastCheckMapper lastCheckMapper;
    @Autowired
    private IProjectManagementService projectManagementService;
    @Autowired
    private ProjectManagementMapper projectManagementMapper;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private IApprovalService approvalService;
    @Autowired
    private IReviewTaskService reviewTaskService;
    @Autowired
    private IYzqFileService fileService;
    @Resource
    private SysUserRoleMapper userRoleMapper;
    /**
     * 查询终验备案管理
     *
     * @param lastCheckId 终验备案管理主键
     * @return 终验备案管理
     */
    @Override
    public LastCheckDetailsVo selectLastCheckByLastCheckId(Long lastCheckId)
    {
        LastCheck lastCheck = lastCheckMapper.selectLastCheckByLastCheckId(lastCheckId);
        LastCheckDetailsVo lastCheckDetailsVo = new LastCheckDetailsVo();
        BeanUtils.copyProperties(lastCheck,lastCheckDetailsVo);
        YzqFile file = fileService.getById(lastCheck.getLastAdoptVoucher());
        if(StringUtils.isNotNull(file)){
            lastCheckDetailsVo.setLastAdoptVoucherFile(file);
        }
        return lastCheckDetailsVo;
    }

    /**
     * 查询终验备案管理列表
     *
     * @param lastCheck 终验备案管理
     * @return 终验备案管理
     */
    @Override
    public List<LastCheck> selectLastCheckList(LastCheck lastCheck)
    {
        return lastCheckMapper.selectLastCheckList(lastCheck);
    }

    /**
     * 新增终验备案管理
     *
     * @param dto 终验备案管理
     * @return 结果
     */
    @Override
    public Long insertLastCheck(LastCheckAddDto dto)
    {
        LastCheck bean = LastCheckAddDto.of(dto);
        //新增终验备案管理-生成对应系统消息
        SysDept sysDept = deptService.selectDeptById(SecurityUtils.getDeptId());
        ProjectManagement projectManagement = projectManagementService.getById(dto.getProId());
        Assert.isTrue(StringUtils.isNotNull(projectManagement),"未找到该项目信息");
        String content=projectManagement.getNickName()+ CreateMsgUtil.SUBMITTED+projectManagement.getName()+CreateMsgUtil.FINAL_ACCEPTANCE_RECORD_SUBMITTED;
        //大数据局收到消息
        List<Long> bigDataId = userRoleMapper.getUserIdByRoleId(Constants.ROLE_TYPE_BIGDATA);
        List<Long> bigDataNormalId = userRoleMapper.getUserIdByRoleId(Constants.ROLE_TYPE_BIGDATA_NORMAL);
        bigDataId.addAll(bigDataNormalId);
        bigDataId.forEach(s->{
            CreateMsgUtil.createMsg(content,s,null,CreateMsgUtil.FINAL_ACCEPTANCE_RECORD_SUBMITTED_TITLE, SysMsg.RESOURCE_TYPE_DATA,null, null);
        });

        //同步到项目信息
        projectManagement.setLastCheckId(bean.getLastCheckId());
        //存储合同扫码件文件
        YzqFile contractFile = dto.getLastAdoptVoucher();
        contractFile.setId(String.valueOf(IdUtils.snowId()));
        contractFile.setDelFlag(ExtractExpert.DELETE_STATUS_NORMAL);
        fileService.save(contractFile);
        bean.setLastAdoptVoucher(contractFile.getId());

        ReviewTask reviewTask = reviewTaskService.getById(projectManagement.getReviewTaskId());
        org.springframework.util.Assert.isTrue(StringUtils.isNotNull(reviewTask),"评审任务不能为空");
        Approval approval = approvalService.getApproval(projectManagement.getProId(), projectManagement.getIsState(),null,null);
        approval.setApprovalNode(ProjectStatusEnum.FINAL_ACCEPTANCE_RECORD_SUBMITTED.get().getNode());
        approval.setApprovalStage(ProjectStatusEnum.FINAL_ACCEPTANCE_RECORD_SUBMITTED.get().getStage());
        approval.setApprovalStatus(ProjectStatusEnum.FINAL_ACCEPTANCE_RECORD_SUBMITTED.get().getStatus());
        approval.setEndTime(LocalDateTime.now());
        approval.setApprovalOperator(SecurityUtils.getNickName());
        projectManagement.setIsState(ProjectStatusEnum.PERFORMANCE_EVALUATION_TO_BE_EVALUATED.get().getStatus());
        projectManagement.setNode(ProjectStatusEnum.PERFORMANCE_EVALUATION_TO_BE_EVALUATED.get().getNode());
        projectManagement.setStag(ProjectStatusEnum.PERFORMANCE_EVALUATION_TO_BE_EVALUATED.get().getStage());
        //新增绩效考评进程
        approvalService.save(new Approval(IdUtils.snowId(),projectManagement.getProId(),ProjectStatusEnum.PERFORMANCE_EVALUATION_TO_BE_EVALUATED.get().getStage(),
                ProjectStatusEnum.PERFORMANCE_EVALUATION_TO_BE_EVALUATED.get().getNode(), LocalDateTime.now(),null, null,ProjectStatusEnum.PERFORMANCE_EVALUATION_TO_BE_EVALUATED.get().getStatus(),
                "/",null, null,null, LocalDateTime.now(),Approval.APPROVAL_TYPE_PROJECT_MANAGE));
        reviewTask.setNode(ProjectStatusEnum.PERFORMANCE_EVALUATION_TO_BE_EVALUATED.get().getNode());
        reviewTask.setNodeStatus(ProjectStatusEnum.PERFORMANCE_EVALUATION_TO_BE_EVALUATED.get().getStatus());
        projectManagementService.updateById(projectManagement);
        reviewTaskService.updateById(reviewTask);
        approvalService.updateById(approval);
        lastCheckMapper.insert(bean);
        //同步项目数据到项目信息表中
        approvalService.synchronizationProjectInfo(projectManagement);
        return bean.getLastCheckId();
    }

    /**
     * 修改终验备案管理
     *
     * @param dto 终验备案管理
     * @return 结果
     */
    @Override
    public int updateLastCheck(LastCheckAddDto dto, Long lastCheckId)
    {
        LastCheck bean = lastCheckMapper.selectById(lastCheckId);
        BeanUtils.copyProperties(dto, bean);
        bean.setUpdateTime(LocalDateTime.now());
        YzqFile file = fileService.getById(bean.getLastAdoptVoucher());
        BeanUtils.copyProperties(dto.getLastAdoptVoucher(),file);
        fileService.updateById(file);
        return lastCheckMapper.updateById(bean);
    }

    /**
     * 删除终验备案管理信息
     *
     * @param lastCheckId 终验备案管理主键
     * @return 结果
     */
    @Override
    public int deleteLastCheckByLastCheckId(Long lastCheckId)
    {
        LastCheck bean = lastCheckMapper.selectById(lastCheckId);
        bean.setIsDelete("1");
        return lastCheckMapper.updateById(bean);
}

    @Override
    public LastCheckTimeVo getLastCheckCreateTime(Long proId) {
        QueryWrapper<ProjectManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pro_id", proId)
                .eq("is_delete", Constants.DELETE_STATUS_NORMAL);
        ProjectManagement projectManagement = projectManagementMapper.selectOne(queryWrapper);
        QueryWrapper<LastCheck> lastCheckQueryWrapper = new QueryWrapper<>();
        lastCheckQueryWrapper.eq("last_check_id",projectManagement.getLastCheckId())
                            .eq("is_delete",Constants.DELETE_STATUS_NORMAL);
        LastCheck lastCheck = lastCheckMapper.selectOne(lastCheckQueryWrapper);
        LastCheckTimeVo lastCheckTimeVo = new LastCheckTimeVo();
        LocalDateTime lastAdoptTime = lastCheck.getLastAdoptTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        lastCheckTimeVo.setLastAdoptTime(lastAdoptTime);
        lastCheckTimeVo.setEndTime(lastAdoptTime.plusMonths(6));
        return lastCheckTimeVo;
    }
}
